
package com.kweative.lingua.demo.controller;

import com.kweative.cluster.Cluster;
import com.kweative.lingua.demo.Demo;
import com.kweative.lingua.demo.model.UserModel;
import com.kweative.lingua.server.Controller;
import com.kweative.lingua.server.annotations.MethodInfo;

/**
 *
 * @author henrik
 */
public class User extends Controller<Demo> {

    @MethodInfo(parms={"user","pass"})
    public boolean authorize(String username,String pass) {
        if (!getApp().users().containsKey(username)) {
            return false;
        }

        UserModel user = getApp().users().get(username);
        
        return user.getPassword().equals(pass);
    }
    
    @MethodInfo(parms={"user","pass"})
    public boolean register(String name,String pass) {
        if (getApp().users().containsKey(name)) {
            return false;
        }
        getApp().users().put(name,new UserModel(name, pass));

        Cluster.store("users",getApp().users());
        
        return true;
    }
    
    @MethodInfo(parms={"user"})
    public boolean unregister(String name) {
        if (!getApp().users().containsKey(name)) {
            return false;
        }
        getApp().users().remove(name);
        
        Cluster.store("users",getApp().users());

        return true;
    }
}
